﻿using System;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

namespace CustomerContactSystem.WebForms
{
    public class Global : System.Web.HttpApplication
    {
        public static ISessionFactory SessionFactory { get; protected set; }

        protected void Application_Start(object sender, EventArgs e)
        {
            var nhConfig = new Configuration().Configure();
            SessionFactory = nhConfig.BuildSessionFactory();
            StructureMapConfigure.Configure();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var session = SessionFactory.OpenSession();
            CurrentSessionContext.Bind(session);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            var session = CurrentSessionContext.Unbind(SessionFactory);
            session.Dispose();
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}